﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Reflection;

namespace QQ2564874169.Core.ORM
{
    internal static class InternalUtils
    {
        internal static IEnumerable<T> ToArray<T>(this IDataReader reader, bool oneRow = false)
        {
            var list = new List<T>();
            var mt = typeof(T);
            if (mt.IsGenericType && mt.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                mt = mt.GetGenericArguments().First();
            }

            var isBaseType = mt.IsPrimitive || mt == typeof(string) || mt == typeof(DateTime) || mt.IsEnum;
            var isExpo = mt == typeof(object);

            while (reader.Read())
            {
                object model = null;
                for (var i = 0; i < reader.FieldCount; i++)
                {
                    var v = reader[i];
                    if (v == null || v is DBNull)
                        continue;

                    if (isBaseType)
                    {
                        model = (T)CheckType(v, mt);
                        break;
                    }
                    if (model == null)
                    {
                        if (isExpo)
                        {
                            model = new ExpandoObject();
                        }
                        else
                        {
                            model = Activator.CreateInstance<T>();
                        }
                    }

                    if (isExpo)
                    {
                        var dict = (IDictionary<string, object>)model;
                        dict.Add(reader.GetName(i), v);
                    }
                    else
                    {
                        var pi = mt.GetProperty(reader.GetName(i),
                            BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance |
                            BindingFlags.IgnoreCase);
                        if (pi != null)
                        {
                            pi.SetValue(model, CheckType(v, pi.PropertyType));
                        }
                    }
                }

                if (model != null)
                {
                    list.Add((T)model);
                }

                if (oneRow)
                {
                    break;
                }
            }

            return list;
        }

        private static object CheckType(object value, Type type)
        {
            if (value == null)
                return null;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                type = new NullableConverter(type).UnderlyingType;
            }

            if (type.IsEnum)
            {
                return Enum.Parse(type, value.ToString());
            }

            if (type == typeof(DateTime) && value.GetType().IsPrimitive)
            {
                var t = long.Parse(value.ToString()).YmdhmsTo();
                if (t.HasValue)
                {
                    return t.Value;
                }
            }
            return Convert.ChangeType(value, type);
        }
    }
}
